// components/request/request.js
const app = getApp()
Component({

  options: {
    styleIsolation: 'apply-shared'
  },

  /**
   * 组件的属性列表
   */
  properties: {
    // 类型：my、received
    type: {
      type: String,
      value: 'my'
    },
    request: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    displayTime: '',
    displayStatus: ''
  },

  lifetimes: {
    attached () {
      this.init()
      this.updateToSee()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 初始化
     */
    init () {
      const type = this.data.type
      const request = this.data.request
      // 请求的状态
      let displayStatus = null
      if (type === 'my') {
        if (request.status === 0) {
          displayStatus = '等待对方回应'
        } else if (request.status === 1) {
          displayStatus = request.type === 0 ? '已被接受，可点击联系对方' : '已被采纳，可点击联系对方'
        } else {
          displayStatus = request.type === 0 ? '已被拒绝' : '未被采纳'
        }
      } else {
        if (request.status === 0) {
          displayStatus = null
        } else if (request.status === 1) {
          displayStatus = request.type === 0 ? '已接受' : '已采纳'
        } else {
          displayStatus = request.type === 0 ? '已拒绝' : '未采纳'
        }
      }
      this.setData({
        displayStatus,
        displayTime: app.formatDate(request.create, 'time')
      })
    },

    /**
     * 更新请求的tosee值
     */
    updateToSee () {
      const type = this.data.type
      const request = this.data.request
      if (type === 'my' && request.tosee) {
        wx.cloud.callFunction({
          name: 'updateRequest',
          data: { operation: 'see', id: request._id }
        })
      }
    },

    /**
     * 接受或采纳
     */
    bindAccept () {
      const _this = this
      const type = this.data.request.type
      const content = type === 0 ? '确定接受该申请吗？' : '确定采纳该线索吗？'
      wx.showModal({
        content,
        success (res) {
          if (res.confirm) {
            const { _id, type } = _this.data.request
            wx.showLoading({ title: '提交中' })
            wx.cloud.callFunction({
              name: 'updateRequest',
              data: { id: _id, operation: 'accept' }
            }).then(res => {
              wx.showToast({
                title: '操作成功',
                icon: 'success'
              })
              _this.setData({ operates: null, displayStatus: type === 0 ? '已接受' : '已采纳' })
            }).catch(err => {
              wx.showToast({
                title: '操作失败'
              })
            })
          }
        }
      })
    },
  
    /**
     * 拒绝或不采纳
     */
    bindReject () {
      const _this = this
      const type = this.data.request.type
      const content = type === 0 ? '确定拒绝该申请吗？' : '确定不采纳该线索吗？'
      wx.showModal({
        content,
        success (res) {
          if (res.confirm) {
            const { _id, type } = _this.data.request
            wx.showLoading({ title: '提交中' })
            wx.cloud.callFunction({
              name: 'updateRequest',
              data: { id: _id, operation: 'reject' }
            }).then(res => {
              wx.showToast({
                title: '操作成功',
                icon: 'success'
              })
              _this.setData({ operates: null, displayStatus: type === 0 ? '已拒绝' : '未采纳' })
            }).catch(err => {
              wx.showToast({
                title: '操作失败'
              })
            })
          }
        }
      })
    },
    
    /**
     * 联系对方
     */
    bindClick () {
      if (this.data.request.status === 1) {
        this.triggerEvent('call')
      }
    },
  }
})
